package com.maxuxu.scihome.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.maxuxu.scihome.mapper.NoticeMapper;
import com.maxuxu.scihome.pojo.Notices;
import com.maxuxu.scihome.pojo.Pages;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @className: NoticeService
 * @author: Maxuxu
 * @date: 2022/7/5 10:20
 * @description: 公告的服务类
 */
@Service
public class NoticeService {
    @Autowired
    Notices notices;
    @Autowired
    NoticeMapper noticeMapper;

    @Autowired
    Pages pages;

    public void insertNotices(Notices notices){
        noticeMapper.insert(notices);
    }

    public Pages selectPage(long current, long size){
        Page<Notices> Page = noticeMapper.selectPage(new Page<>(current, size), null);
        List<Notices> records = Page.getRecords();
        long total = Page.getTotal();
        long pages = Page.getPages();
        long current1 = Page.getCurrent();
        long size1 = Page.getSize();
        this.pages.setPages(pages);
        this.pages.setCurrent(current1);
        this.pages.setSize(size1);
        this.pages.setTotal(total);
        this.pages.setRecords(records);
        return this.pages;
    }


    public Notices selectByAny(Notices notices){
        QueryWrapper<Notices> wrapper = new QueryWrapper<>();
        wrapper.eq("title", notices.getTitle());
        Notices notices1 = noticeMapper.selectOne(wrapper);
        return notices1;
    }

    public List<Notices> selectByLike(String content){
        QueryWrapper<Notices> queryWrapper = new QueryWrapper<>();
        // like 表示包含某个字符
        // likeLeft 表示以某个字符结尾
        // likeRight 表示以某个字符开头的
        queryWrapper.like("content",content);
        List<Notices> noticesList = noticeMapper.selectList(queryWrapper);
        return noticesList;
    }

    public void deletnotices(List<Integer> list){
        noticeMapper.deleteBatchIds(list);
    }

    public void updateNotice(String oldname,Notices notices){
        UpdateWrapper<Notices> wrapper = new UpdateWrapper<>();
        wrapper.eq("title", oldname);
        noticeMapper.update(notices, wrapper);

    }


}
